﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Achievement.DataModels;
using Achievement.Common;

namespace Achievement.BLL.Provider
{
    public class UsersProvider
    {
        private static readonly UsersProvider instance = new UsersProvider();
        public static UsersProvider Instance
        {
            get { return instance; }
        }
        private UsersProvider() { }

        public void AddUser(Users users)
        {
            using (CGKEntities CGK = new CGKEntities())
            {
                users.PassWord = ATools.MD5Encrypt(users.PassWord);
                CGK.Users.AddObject(users);
                CGK.SaveChanges();
            }
        }

        public void DeleteUser(long id)
        {
            using (CGKEntities CGK = new CGKEntities())
            {
                Users u = CGK.Users.Where(user => user.ID == id).FirstOrDefault<Users>();
                if (u != null)
                {
                    CGK.Users.DeleteObject(u);
                    CGK.SaveChanges();
                }
            }
        }

        public Users GetUserById(long id)
        {
            using (CGKEntities CGK = new CGKEntities())
            {
                return CGK.Users.Where(user => user.ID == id).FirstOrDefault<Users>();
            }
        }

        public List<Users> GetUserList(string where, int page, int rows, string sord, string sidx, out int totalRecords)
        {
            string sql = @"SELECT * FROM [Users] AS [a] WHERE Display=1";
            if (!string.IsNullOrEmpty(where))
            {
                sql += " and (a.Name like '%" + where + "%' or a.LoginName like '%" + where + "%')";
            }
            using (CGKEntities CGK = new CGKEntities())
            {
                var query = CGK.ExecuteStoreQuery<Users>(sql).AsQueryable();
                totalRecords = query.Count();
            }
            using (CGKEntities CGK = new CGKEntities())
            {
                var query = CGK.ExecuteStoreQuery<Users>(sql).AsQueryable();
                var list = DBHelper.SortingAndPaging<Users>(query, sidx, sord, page - 1, rows).ToList();
                return list;
            }
        }

        public List<Users> GetUserList()
        {
            using (CGKEntities CGK = new CGKEntities())
            {
                return CGK.Users.Where(u => u.IsActive == true && u.Display == true).ToList();
            }
        }

        public void SaveUser(Users user)
        {
            using (CGKEntities CGK = new CGKEntities())
            {
                //Users u = CGK.Users.Where(oU=>oU.ID==user.ID).FirstOrDefault<Users>();
                //CGK.Users.ApplyCurrentValues(user);
                CGK.Users.Attach(user);
                CGK.ObjectStateManager.ChangeObjectState(user,System.Data.EntityState.Modified);
                CGK.SaveChanges();
            }
        }

        public Users GetUserByLoginName(string loginName)
        {
            using (CGKEntities CGK = new CGKEntities())
            {
                return CGK.Users.Where(u=>u.LoginName == loginName).FirstOrDefault();
            }
        }

        public void UserAuditing(long uid, bool auditing)
        {
            using (CGKEntities CGK = new CGKEntities())
            {
                var user = CGK.Users.Where(u => u.ID == uid).FirstOrDefault();
                if (user != null)
                {
                    user.IsActive = auditing;
                }
                CGK.SaveChanges();
            }
        }

        public long GetUserBaseCount()
        {
            using (CGKEntities CGK = new CGKEntities())
            {
                var ubc = CGK.UserBaseCount.FirstOrDefault();
                if (ubc != null)
                {
                    return ubc.Count;
                }
                return 0;
            }
        }

        public void SetUserBaseCount(long count)
        {
            using (CGKEntities CGK = new CGKEntities())
            {
                var ubc = CGK.UserBaseCount.FirstOrDefault();
                if (ubc == null)
                {
                    UserBaseCount add = new UserBaseCount() { Count = count };
                    CGK.UserBaseCount.AddObject(add);
                }
                else
                {
                    ubc.Count = count;
                }
                CGK.SaveChanges();
            }
        }
    }
}
